//
//  BorrowViewController.m
//  ZYZB
//
//  Created by ENERGY on 2018/7/10.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "BorrowViewController.h"
#import "LoansSuccessView.h"
#import "CityPickerView.h"

static int i = 59;

@interface BorrowViewController ()
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)UIView * headerView;
@property (nonatomic, strong)UITextField * nameTextField;//姓名
@property (nonatomic, strong)UITextField * careerTextField;//从业年限
@property (nonatomic, strong)UITextField * storeTextField;//门店门数
@property (nonatomic, strong)UITextField * loansTextField;//申请贷款金额
@property (nonatomic, strong)UITextField * phoneTextField;//电话号码
@property (nonatomic, strong)UITextField * verificationCodeTextField;//手机验证码
@property (nonatomic, strong)UILabel * censusRegisterContentLab;//户籍显示文本
@property (nonatomic, strong)UILabel * managePlaceContentLab;//主要经营所在地显示文本
@property (nonatomic, strong)UIButton * sendVerificationCodeBtn;//发送验证码按钮
@property (nonatomic, strong)NSTimer * timer;//计时器
@end

@implementation BorrowViewController(networking)
//发送验证码
- (void)sendMessageCode{
    NSString * timeStr = [self currentTimeStr];
    //str
    NSDictionary * dic = @{@"phone":self.phoneTextField.text,
                           @"t":timeStr
                           };
    //sign
    NSDictionary * signDic = @{@"phone":self.phoneTextField.text,
                               @"t":timeStr,
                               @"merchantNo":@"80d709a97af1b8ab"
                               };
    SortMD5Code * md5Code = [[SortMD5Code alloc] init];
    NSString * jsonStr = [md5Code getJsonWithStrDic:dic withSignDic:signDic];
    NSData *postData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:@"http://120.78.157.132:82/index.php/sendCode/index?api=api1001"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //设置请求方式
    request.HTTPMethod = @"POST";
    //设置请求参数
    request.HTTPBody = postData;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        [self hiddenProgressHUD];
        if (connectionError) {
            if (connectionError.code == -1001) {
                [self.view makeToast:@"请求网络超时，请稍后再试" duration:1 position:CSToastPositionCenter];
            }
        }else{
            NSDictionary *dictionary =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];//data解析为dictionary
            if (!dictionary) {
                NSLog(@"数据缺失");
                return;
            }
            //        NSString * compareMD5Str = [md5Code encodeStringWithDictionary:dictionary];
            //        NSString * signResult = [dictionary objectForKey:@"sign"];//接到的json串sign字段
            //        NSDictionary * jsonData = [md5Code getNSDictionaryData:dictionary];//解析为字典数据
            //        if ([compareMD5Str isEqualToString:signResult]) {
            //            NSLog(@"成功");
            //        }else{
            //            NSLog(@"失败");
            //        }
        }
    }];
}
@end
@implementation BorrowViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //返回按钮
    UIButton * backBtn = [[UIButton alloc] initWithFrame:CGRectMake(18/WIDTH_6_SCALE, 14/WIDTH_6_SCALE + 24 + 20, 24/WIDTH_6_SCALE, 30/WIDTH_6_SCALE)];
    [backBtn addTarget:self action:@selector(backBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setImage:[UIImage imageNamed:@"out"] forState:UIControlStateNormal];
    UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    self.navigationItem.title = @"我要借款";
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.view addSubview:self.tableView];
}
#pragma mark - init
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVI_HEIGHT - BOTTOM_HEIGHT) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.tableHeaderView = self.headerView;
        _tableView.tableFooterView = [UIView new];
    }return _tableView;
}

- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 600/WIDTH_6_SCALE)];
        _headerView.backgroundColor = [UIColor whiteColor];
        int leftBorderW = 18/WIDTH_6_SCALE;
        int topBorderH = 9/WIDTH_6_SCALE;
        int titleW = 97/WIDTH_6_SCALE;
        int lastLabW = 30/WIDTH_6_SCALE;
        //姓名框
        UIView * nameBGView = [[UIView alloc] initWithFrame:CGRectMake(leftBorderW, 10/WIDTH_6_SCALE, SCREEN_WIDTH - 2*leftBorderW, 44/WIDTH_6_SCALE)];
        [self view:nameBGView withBackgroundColor:[UIColor whiteColor] withBorderColor:getColor(borderColor) withBorderWidth:1 withCornerRadius:6 withMasksToBounds:YES withSuperView:_headerView];
        UILabel * nameTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, titleW, nameBGView.height)];
        [self label:nameTitleLab withFont:DEF_FontSize_10 withText:@"姓名" withTextColor:[UIColor blackColor] withTextAlignment:NSTextAlignmentCenter withView:nameBGView];
        UIView * nameLineView = [[UIView alloc] initWithFrame:CGRectMake(nameTitleLab.right, topBorderH, 1/WIDTH_6_SCALE, nameBGView.height - topBorderH*2)];
        nameLineView.backgroundColor = getColor(borderColor);
        [nameBGView addSubview:nameLineView];
        //姓名填写
        self.nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(nameLineView.right + 5/WIDTH_6_SCALE, 0, nameBGView.width - nameLineView.right - 5/WIDTH_6_SCALE, nameBGView.height)];
        self.nameTextField.placeholder = @"请输入真实姓名";
        self.nameTextField.font = DEF_FontSize_10;
        [nameBGView addSubview:self.nameTextField];
        //户籍框
        UIView * censusRegisterBGView = [[UIView alloc] initWithFrame:CGRectMake(nameBGView.left, nameBGView.bottom + 12/WIDTH_6_SCALE, nameBGView.width, nameBGView.height)];
        [self view:censusRegisterBGView withBackgroundColor:[UIColor whiteColor] withBorderColor:getColor(borderColor) withBorderWidth:1 withCornerRadius:6 withMasksToBounds:YES withSuperView:_headerView];
        UILabel * censusRegisterTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, titleW, nameBGView.height)];
        [self label:censusRegisterTitleLab withFont:DEF_FontSize_10 withText:@"户籍地址" withTextColor:[UIColor blackColor] withTextAlignment:NSTextAlignmentCenter withView:censusRegisterBGView];
        UIView * censusRegisterLineView = [[UIView alloc] initWithFrame:CGRectMake(censusRegisterTitleLab.right, topBorderH, 1/WIDTH_6_SCALE, nameLineView.height)];
        censusRegisterLineView.backgroundColor = getColor(borderColor);
        [censusRegisterBGView addSubview:censusRegisterLineView];
        //户籍显示文本
        self.censusRegisterContentLab = [[UILabel alloc] initWithFrame:CGRectMake(censusRegisterLineView.right, 0, censusRegisterBGView.width - censusRegisterLineView.right, censusRegisterBGView.height)];
        [self label:self.censusRegisterContentLab withFont:DEF_FontSize_10 withText:@"" withTextColor:[UIColor blackColor] withTextAlignment:NSTextAlignmentCenter withView:censusRegisterBGView];
        self.censusRegisterContentLab.userInteractionEnabled = YES;
        UITapGestureRecognizer *censusRegisterTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(censusRegisterContentLabTap)];
        [self.censusRegisterContentLab addGestureRecognizer:censusRegisterTap];
        //从业年限框
        UIView * careerBGView = [[UIView alloc] initWithFrame:CGRectMake(nameBGView.left, censusRegisterBGView.bottom + 12/WIDTH_6_SCALE, nameBGView.width, nameBGView.height)];
        [self view:careerBGView withBackgroundColor:[UIColor whiteColor] withBorderColor:getColor(borderColor) withBorderWidth:1 withCornerRadius:6 withMasksToBounds:YES withSuperView:_headerView];
        UILabel * careerTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, titleW, nameBGView.height)];
        [self label:careerTitleLab withFont:DEF_FontSize_10 withText:@"从业年限" withTextColor:[UIColor blackColor] withTextAlignment:NSTextAlignmentCenter withView:careerBGView];
        UIView * careerLineView = [[UIView alloc] initWithFrame:CGRectMake(careerTitleLab.right, topBorderH, 1/WIDTH_6_SCALE, nameLineView.height)];
        careerLineView.backgroundColor = getColor(borderColor);
        [careerBGView addSubview:careerLineView];
        //从业年限填写
        self.careerTextField = [[UITextField alloc] initWithFrame:CGRectMake(careerLineView.right + 5/WIDTH_6_SCALE, 0, nameBGView.width - careerLineView.right - 5/WIDTH_6_SCALE, nameBGView.height)];
        self.careerTextField.placeholder = @"请填写从业年限，例如3";
        self.careerTextField.font = DEF_FontSize_10;
        [careerBGView addSubview:self.careerTextField];
        UILabel * yearTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(careerBGView.width - lastLabW, 0, lastLabW, careerBGView.height)];
        [self label:yearTitleLab withFont:DEF_FontSize_10 withText:@"年" withTextColor:getColor(@"3675bd") withTextAlignment:NSTextAlignmentCenter withView:careerBGView];
        //门店门数框
        UIView * storeBGView = [[UIView alloc] initWithFrame:CGRectMake(nameBGView.left, careerBGView.bottom + 12/WIDTH_6_SCALE, nameBGView.width, nameBGView.height)];
        [self view:storeBGView withBackgroundColor:[UIColor whiteColor] withBorderColor:getColor(borderColor) withBorderWidth:1 withCornerRadius:6 withMasksToBounds:YES withSuperView:_headerView];
        UILabel * storeTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, titleW, nameBGView.height)];
        [self label:storeTitleLab withFont:DEF_FontSize_10 withText:@"门店门数" withTextColor:[UIColor blackColor] withTextAlignment:NSTextAlignmentCenter withView:storeBGView];
        UIView * storeLineView = [[UIView alloc] initWithFrame:CGRectMake(storeTitleLab.right, topBorderH, 1/WIDTH_6_SCALE, nameLineView.height)];
        storeLineView.backgroundColor = getColor(borderColor);
        [storeBGView addSubview:storeLineView];
        //从业年限填写
        self.storeTextField = [[UITextField alloc] initWithFrame:CGRectMake(storeLineView.right + 5/WIDTH_6_SCALE, 0, nameBGView.width - storeLineView.right - 5/WIDTH_6_SCALE, storeBGView.height)];
//        self.storeTextField.backgroundColor = [UIColor orangeColor];
        self.storeTextField.placeholder = @"请填写门店数量，例如4";
        self.storeTextField.font = DEF_FontSize_10;
        [storeBGView addSubview:self.storeTextField];
        UILabel * storeNumTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(storeBGView.width - lastLabW, 0, lastLabW, careerBGView.height)];
        [self label:storeNumTitleLab withFont:DEF_FontSize_10 withText:@"个" withTextColor:getColor(@"3675bd") withTextAlignment:NSTextAlignmentCenter withView:storeBGView];
        //主要经营所在地框
        UIView * managePlaceBGView = [[UIView alloc] initWithFrame:CGRectMake(nameBGView.left, storeBGView.bottom + 12/WIDTH_6_SCALE, nameBGView.width, nameBGView.height)];
        [self view:managePlaceBGView withBackgroundColor:[UIColor whiteColor] withBorderColor:getColor(borderColor) withBorderWidth:1 withCornerRadius:6 withMasksToBounds:YES withSuperView:_headerView];
        UILabel * managePlaceTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, titleW, nameBGView.height)];
        [self label:managePlaceTitleLab withFont:DEF_FontSize_10 withText:@"主要经营所在地" withTextColor:[UIColor blackColor] withTextAlignment:NSTextAlignmentCenter withView:managePlaceBGView];
        UIView * managePlaceLineView = [[UIView alloc] initWithFrame:CGRectMake(managePlaceTitleLab.right, topBorderH, 1/WIDTH_6_SCALE, nameLineView.height)];
        managePlaceLineView.backgroundColor = getColor(borderColor);
        [managePlaceBGView addSubview:managePlaceLineView];
        //主要经营所在地文本
        self.managePlaceContentLab = [[UILabel alloc] initWithFrame:CGRectMake(managePlaceLineView.right, 0, managePlaceBGView.width - managePlaceLineView.right, managePlaceBGView.height)];
        [self label:self.managePlaceContentLab withFont:DEF_FontSize_10 withText:@"" withTextColor:[UIColor blackColor] withTextAlignment:NSTextAlignmentCenter withView:managePlaceBGView];
        self.managePlaceContentLab.userInteractionEnabled = YES;
        UITapGestureRecognizer *managePlaceTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(managePlaceContentLabTap)];
        [self.managePlaceContentLab addGestureRecognizer:managePlaceTap];
        //申请贷款金额框
        UIView * loansBGView = [[UIView alloc] initWithFrame:CGRectMake(nameBGView.left, managePlaceBGView.bottom + 12/WIDTH_6_SCALE, nameBGView.width, nameBGView.height)];
        [self view:loansBGView withBackgroundColor:[UIColor whiteColor] withBorderColor:getColor(borderColor) withBorderWidth:1 withCornerRadius:6 withMasksToBounds:YES withSuperView:_headerView];
        UILabel * loansTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, titleW, nameBGView.height)];
        [self label:loansTitleLab withFont:DEF_FontSize_10 withText:@"申请贷款金额" withTextColor:[UIColor blackColor] withTextAlignment:NSTextAlignmentCenter withView:loansBGView];
        UIView * loansLineView = [[UIView alloc] initWithFrame:CGRectMake(loansTitleLab.right, topBorderH, 1/WIDTH_6_SCALE, nameLineView.height)];
        loansLineView.backgroundColor = getColor(borderColor);
        [loansBGView addSubview:loansLineView];
        //申请贷款金额填写
        self.loansTextField = [[UITextField alloc] initWithFrame:CGRectMake(loansLineView.right + 5/WIDTH_6_SCALE, 0, nameBGView.width - loansLineView.right - 5/WIDTH_6_SCALE, storeBGView.height)];
//        self.loansTextField.backgroundColor = [UIColor orangeColor];
        self.loansTextField.placeholder = @"请填写借款金额";
        self.loansTextField.font = DEF_FontSize_10;
        [loansBGView addSubview:self.loansTextField];
        UILabel * loansNumTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(loansBGView.width - lastLabW, 0, lastLabW, loansBGView.height)];
        [self label:loansNumTitleLab withFont:DEF_FontSize_10 withText:@"万元" withTextColor:getColor(@"3675bd") withTextAlignment:NSTextAlignmentCenter withView:loansBGView];
        //手机号框
        UIView * phoneBGView = [[UIView alloc] initWithFrame:CGRectMake(nameBGView.left, loansBGView.bottom + 12/WIDTH_6_SCALE, nameBGView.width, nameBGView.height)];
        [self view:phoneBGView withBackgroundColor:[UIColor whiteColor] withBorderColor:getColor(borderColor) withBorderWidth:1 withCornerRadius:6 withMasksToBounds:YES withSuperView:_headerView];
        UILabel * phoneTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, titleW, nameBGView.height)];
        [self label:phoneTitleLab withFont:DEF_FontSize_10 withText:@"手机号" withTextColor:[UIColor blackColor] withTextAlignment:NSTextAlignmentCenter withView:phoneBGView];
        UIView * phoneLineView = [[UIView alloc] initWithFrame:CGRectMake(phoneTitleLab.right, topBorderH, 1/WIDTH_6_SCALE, nameLineView.height)];
        phoneLineView.backgroundColor = getColor(borderColor);
        [phoneBGView addSubview:phoneLineView];
        //手机号填写
        self.phoneTextField = [[UITextField alloc] initWithFrame:CGRectMake(phoneLineView.right + 5/WIDTH_6_SCALE, 0, phoneBGView.width - phoneLineView.right - 5/WIDTH_6_SCALE, phoneBGView.height)];
        self.phoneTextField.placeholder = @"请输入11位手机号码";
        self.phoneTextField.font = DEF_FontSize_10;
        [phoneBGView addSubview:self.phoneTextField];
        //手机验证码框
        UIView * verificationCodeBGView = [[UIView alloc] initWithFrame:CGRectMake(nameBGView.left, phoneBGView.bottom + 12/WIDTH_6_SCALE, nameBGView.width, nameBGView.height)];
        [self view:verificationCodeBGView withBackgroundColor:[UIColor whiteColor] withBorderColor:getColor(borderColor) withBorderWidth:1 withCornerRadius:6 withMasksToBounds:YES withSuperView:_headerView];
        UILabel * verificationCodeTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, titleW, nameBGView.height)];
        [self label:verificationCodeTitleLab withFont:DEF_FontSize_10 withText:@"手机验证码" withTextColor:[UIColor blackColor] withTextAlignment:NSTextAlignmentCenter withView:verificationCodeBGView];
        UIView * verificationCodeLineView = [[UIView alloc] initWithFrame:CGRectMake(phoneTitleLab.right, topBorderH, 1/WIDTH_6_SCALE, nameLineView.height)];
        verificationCodeLineView.backgroundColor = getColor(borderColor);
        [verificationCodeBGView addSubview:verificationCodeLineView];
        //发送验证码
        UIButton * sendVerificationCodeBtn = [[UIButton alloc] initWithFrame:CGRectMake(verificationCodeBGView.width - 79/WIDTH_6_SCALE - 10/WIDTH_6_SCALE, 6/WIDTH_6_SCALE, 79/WIDTH_6_SCALE, verificationCodeBGView.height - (6*2)/WIDTH_6_SCALE)];
        [sendVerificationCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [sendVerificationCodeBtn setTitleColor:getColor(@"3675bd") forState:UIControlStateNormal];
        sendVerificationCodeBtn.titleLabel.font = DEF_FontSize_12;
        [sendVerificationCodeBtn addTarget:self action:@selector(sendVerificationCodeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self button:sendVerificationCodeBtn withBGColor:[UIColor whiteColor] withLayerBorderColor:getColor(@"3675bd") withLayerBorderWith:1 withCornerRadius:6 withMasksToBounds:YES withViwe:verificationCodeBGView];
        self.sendVerificationCodeBtn = sendVerificationCodeBtn;
        //验证码填写
        self.verificationCodeTextField = [[UITextField alloc] initWithFrame:CGRectMake(verificationCodeLineView.right + 5/WIDTH_6_SCALE, 0, verificationCodeBGView.width - sendVerificationCodeBtn.width - 10/WIDTH_6_SCALE - verificationCodeLineView.left - 5/WIDTH_6_SCALE, verificationCodeBGView.height)];
        self.verificationCodeTextField.placeholder = @"请填写手机验证码";
        self.verificationCodeTextField.font = DEF_FontSize_10;
        [verificationCodeBGView addSubview:self.verificationCodeTextField];
        //提交
        UIButton * submitBtn = [[UIButton alloc] initWithFrame:CGRectMake(58/WIDTH_6_SCALE, verificationCodeBGView.bottom + 55/WIDTH_6_SCALE, SCREEN_WIDTH - (58*2)/WIDTH_6_SCALE, 38/WIDTH_6_SCALE)];
        [self button:submitBtn withBGColor:getColor(@"3675bd") withLayerBorderColor:nil withLayerBorderWith:0 withCornerRadius:6 withMasksToBounds:YES withViwe:_headerView];
        [submitBtn addTarget:self action:@selector(submitBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [submitBtn setTitle:@"提交申请" forState:UIControlStateNormal];
        [submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        _headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, submitBtn.bottom + 55/WIDTH_6_SCALE);
    }return _headerView;
}
#pragma mark - action
- (void)sendVerificationCodeBtnClick:(UIButton *)sender{
    NSLog(@"获取验证码");
    if ([self valiMobile:self.phoneTextField.text]) {
        //获取验证码
        [self sendMessageCode];
        //初始化定时器
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerCountDown:) userInfo:nil repeats:YES];
    }else{
        [self.view makeToast:@"请正确填写手机号码" duration:1 position:CSToastPositionCenter style:nil];
        return;
    }
}
- (void)timerCountDown:(NSTimer *)timmer{
    NSString *str = [NSString stringWithFormat:@"%d后重试",i];
    self.sendVerificationCodeBtn.titleLabel.text = str;
    self.sendVerificationCodeBtn.userInteractionEnabled = NO;
    i--;
    if (i == -1) {
        [timmer invalidate];
        self.sendVerificationCodeBtn.titleLabel.text = @"获取验证码";
        self.sendVerificationCodeBtn.userInteractionEnabled = YES;
        i = 59;
    }
}
- (void)backBtnClick:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)submitBtnClick:(UIButton *)sender{
    LoansSuccessView * loansView = [[LoansSuccessView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [loansView showView];
}
- (void)censusRegisterContentLabTap{
    NSLog(@"户籍地址");
    CityPickerView * pickerView = [[CityPickerView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - BOTTOM_HEIGHT)];
    pickerView.getAddress = ^(NSString * completeStr, NSString * specificStr){
        self.censusRegisterContentLab.text = completeStr;
    };
    [pickerView showView];
}
- (void)managePlaceContentLabTap{
    NSLog(@"主要经营所在地");
    CityPickerView * pickerView = [[CityPickerView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - BOTTOM_HEIGHT)];
    pickerView.getAddress = ^(NSString * completeStr, NSString * specificStr){
        self.managePlaceContentLab.text = completeStr;
    };
    [pickerView showView];
}
@end
